package nl.hu.verbrandendephoenix.view;

import java.util.ArrayList;

import nl.hu.verbrandendephoenix.shared.Bid;

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.visualization.client.AbstractDataTable;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.visualizations.corechart.BarChart;
import com.google.gwt.visualization.client.visualizations.corechart.ColumnChart;
import com.google.gwt.visualization.client.visualizations.corechart.LineChart;
import com.google.gwt.visualization.client.visualizations.corechart.Options;

public class BidStats {

	private Panel pnlBidStats;

	public BidStats() {
		pnlBidStats = new VerticalPanel();
		pnlBidStats.addStyleName("post");
	}

	public Panel constructForm() {
		pnlBidStats = new VerticalPanel();
		return pnlBidStats;
	}

	private Options createOptions() {
		Options options = Options.create();
		options.setWidth(550);
		options.setHeight(400);
		options.setTitle("Biedingen per datum");
		options.setBackgroundColor("#DAD5D1");
		return options;
	}

	public void createChart(final ArrayList<Bid> bids) {
		Runnable onLoadCallback = new Runnable() {
			public void run() {
				int counter = 0;

				DataTable data = DataTable.create();
				data.addColumn(ColumnType.DATE, "Datum");
				data.addColumn(ColumnType.NUMBER, "Bod");
				data.addRows(bids.size() * 2);

				for(Bid b : bids) {
					data.setValue(counter, 0, b.getDate());
					data.setValue(counter, 1, b.getPrice());
					counter++;
				}

				// Create a pie chart visualization.
				LineChart line = new LineChart(data, createOptions());
				pnlBidStats.add(line);
			}
		};

		// Load the visualization api, passing the onLoadCallback to be called
		// when loading is done.
		VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE);
	}
}
